package com.wss.module.wan.ui.project;

import com.wss.common.base.adapter.BaseListAdapter;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.base.adapter.superadapter.SuperViewHolder;
import com.wss.common.utils.ColorUtils;
import com.wss.common.utils.PxUtils;
import com.wss.module.wan.ResourceTable;
import com.wss.module.wan.bean.Classification;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class TypePopupWindow extends PopupDialog {

    private Context mContext;
    private Component parent;
    private ListContainer recyclerView;
    private List<Classification> data;
    private Classification classification;//当前显示的分类
    private OnProjectTypeClickListener listener;

    public TypePopupWindow(Context context) {
        super(context, null);
        this.mContext = context;
        setBackColor(Color.TRANSPARENT);
        setHasArrow(true);
    }

    public TypePopupWindow(Context context, Component contentComponent) {
        this(context, contentComponent,
                PxUtils.getScreenWidth(context),
                PxUtils.getScreenHeight(context));
    }

    public TypePopupWindow(Context context, Component contentComponent, int width, int height) {
        super(context, contentComponent, width, height);
        this.mContext = context;
        setBackColor(Color.TRANSPARENT);
        setHasArrow(true);
    }

    public TypePopupWindow setData(List<Classification> data) {
        this.data = data;
        return this;
    }

    public TypePopupWindow setCurrent(Classification classification) {
        this.classification = classification;
        return this;
    }


    public TypePopupWindow setListener(OnProjectTypeClickListener listener) {
        this.listener = listener;
        return this;
    }

    public void show(Component view) {
        this.parent = view;
        initView();
    }

    private void initView() {

        Component childView = LayoutScatter.getInstance(mContext)
                .parse(ResourceTable.Layout_wan_pop_project_pop,
                        null,
                        false);
        childView.findComponentById(ResourceTable.Id_view).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                hide();
            }
        });
        recyclerView = (ListContainer) childView.findComponentById(ResourceTable.Id_recycle_view);
        setCustomComponent(childView);
        setMode(LayoutAlignment.LEFT);
        show();
        initList();
    }

    private void initList() {
        recyclerView.setItemProvider(new TypeAdapter(mContext, data, new OnListItemClickListener<Classification>() {
            @Override
            public void onItemClick(Classification data, int position) {
                hide();
                if (listener != null) {
                    listener.onProjectTypeClick(data);
                }
            }
        }));
    }

    class TypeAdapter extends BaseListAdapter<Classification> {

        private Context context;

        /**
         * 常规列表重写该方法
         *
         * @param context     context
         * @param list        数据源
         * @param listener    Item点击回调
         */
        public TypeAdapter(Context context, List<Classification> list, OnListItemClickListener<Classification> listener) {
            super(context, list, ResourceTable.Layout_wan_item_of_setup_list, listener);
            this.context = context;
        }

        @Override
        public void onBindData(@NotNull SuperViewHolder holder, int layoutPosition, @NotNull Classification data) {
            Text textView = holder.findViewById(ResourceTable.Id_tv_text);
            if (classification != null && classification.getId() == data.getId()) {
                textView.setTextColor(ColorUtils.colorResToColor(context, ResourceTable.Color_white));
                textView.setBackground(ColorUtils.colorResToElement(context, ResourceTable.Color_theme));
            } else {
                textView.setTextColor(ColorUtils.colorResToColor(context, ResourceTable.Color_black));
                textView.setBackground(ColorUtils.colorResToElement(context, ResourceTable.Color_transparent));
            }
            textView.setText(data.getName());
        }
    }

    interface OnProjectTypeClickListener {
        void onProjectTypeClick(Classification classification);
    }
}
